package com.hong.qrpc.example.api;

import com.alibaba.nacos.api.exception.NacosException;
import com.hong.qrpc.RpcClient;
import com.hong.qrpc.example.service.CalcService;

import java.io.IOException;
import java.util.Scanner;

/**
 * @Author HQT
 * @Date 2022/7/10 20:30
 */
public class Client {
    public static void main(String[] args) throws NacosException, IOException {
        //生成客户端，客户端会自动检查有没有qrpc.properties配置文件，如果有的话就会自动加载，没有的话则使用默认配置
        RpcClient rpcClient = new RpcClient();
        //通过客户端对象传入代理接口类和服务提供者直连地址直接生成代理对象
        CalcService service = rpcClient.getProxy(CalcService.class, "127.0.0.1:3000");
        while (!new Scanner(System.in).next().equals("0")) {
            int r1 = service.add(1, 2);
            int r2 = service.minus(2, 1);
            System.out.println(r1 + "  " + r2);
        }
        //测试Object方法的调用
        System.out.println("service eq obj? =" + service.equals(new Object()) + "service eq service?:" + service.equals(service));
        System.out.println("service toString = " + service);
        System.out.println("service hashCode = " + service.hashCode());
        synchronized (service) {
            new Thread(() -> {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (service) {
                    service.notifyAll();
                }
            }).start();
            try {
                service.wait();
                System.out.println("service getClass = " + service.getClass());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //关闭客户端
        rpcClient.stop();
    }
}
